package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
//定义Topic接口，注入topicConfig的主题名称
@Topic(TopicConfig.VMS_SUPPLY_TOPIC)
//TODO：实现MsgHandler就是为了重写process方法
public class SupplyMsgHandler implements MsgHandler {

    //自动注入收获机服务
    @Autowired
    VendingMachineService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //1.解析补货协议
        SupplyContract supplyContract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        //2.更新售货机库存
        vmService.supply(supplyContract);
    }
}
